package com.nursinghome.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nursinghome.admin.service.ElderlyService;
import com.nursinghome.admin.service.MedicalStaffService;
import com.nursinghome.bean.Elderly;
import com.nursinghome.bean.RespBean;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.group.ElderlyUpdateGroup;
import com.nursinghome.group.ElderlyUpdateGroupByAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/elder")
public class ElderlyController {
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private MedicalStaffService medicalStaffService;
    @Value("${pageSize}")
    private Integer pageSize;
    //获取到所有的老人
    @GetMapping
    public RespBean selectAll(Integer pageNum, String ename, String eusername) {
        //如何pageNum为空默认查询第一页
        if (pageNum == null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Elderly> elderlies = elderlyService.selectAll(ename, eusername);
        PageInfo<Elderly> elderlyPageInfo = new PageInfo<>(elderlies);
        return RespBean.ok("",elderlyPageInfo);
    }
    //新增老人
    @PostMapping
    public RespBean insertElder(@RequestBody @Validated({ElderlyUpdateGroup.class, Default.class}) Elderly elderly) throws NurisingHomeException {
        if (elderlyService.addElderly(elderly)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改老人
    @PutMapping
    public RespBean updateElder(@RequestBody @Validated({ElderlyUpdateGroup.class, ElderlyUpdateGroupByAdmin.class}) Elderly elderly) throws NurisingHomeException {
        if (elderlyService.update(elderly)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除老人
    @DeleteMapping("/{eid}")
    public RespBean delElder(@PathVariable("eid") Long eid) throws NurisingHomeException {
        if (elderlyService.delByEid(eid)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显老人数据
    @GetMapping("/{eid}")
    public RespBean selectByEid(@PathVariable("eid") Long eid) throws NurisingHomeException {
        Elderly elderly = elderlyService.selectByEid(eid);
        return RespBean.ok("",elderly);
    }
    //查询所有的员工
    @GetMapping("/allmedical")
    public RespBean selectAllMedical(String mname){
        List<HashMap<String, Object>> list = medicalStaffService.selectAll(mname,"","")
                .stream()
                .filter(medicalStaff -> medicalStaff.getMstate() == 1)
                .map(medicalStaff -> {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("key", medicalStaff.getMid());
                    map.put("label", medicalStaff.getMno() + "-" + medicalStaff.getMname());
                    return map;
                })
                .collect(Collectors.toList());

        return RespBean.ok("",list);
    }
    //给老人分配护工
    @PostMapping("/allmedical")
    public RespBean insertAddMedical(@RequestBody Map<String, Object> map){
        Long eid = new Long((Integer)map.get("eid") + "");
        ArrayList<Integer> midslist = (ArrayList<Integer>) map.get("mids");
        Integer[] mids = midslist.toArray(new Integer[midslist.size()]);

        elderlyService.assignCaregivers(eid, mids);

        return RespBean.ok("分配成功");
    }
    //根据老人id查询所有的护工id
    @GetMapping("/allMedicalIds/{eid}")
    public RespBean selectByEidAndEM(@PathVariable("eid") Long eid){
        Integer[] mids = elderlyService.selectByEidAndEM(eid);
        return RespBean.ok("",mids) ;
    }
}
